<?php 

class Alipay extends BaseModel
{


function payTo($payment,$returnurl,$pay){
		$charset = 'gbk';
        $service = 'create_direct_pay_by_user';
        $agent = 'C4335319945672464113';
        $parameter = array(
            'agent'             => $agent,
            'service'           => $service,  //交易类型
            'partner'           => $payment['alipay_partner'], //合作商户号
            '_input_charset'    => $charset,
            'notify_url'        => "$returnurl",
            'return_url'        => "$returnurl", //同步返回           
            'subject'           => $pay['name'], //商品名称，必填
            'out_trade_no'      => $pay['orderid'], //商品外部交易号，必填（保证唯一性）
            'price'             => $pay['price'],  //总额
			'body'				=> $pay['name'], //商品描述，必填
            'quantity'          => 1,  //商品数量,强制为1,总额在price处算好
            'payment_type'      => 1,  //默认为1,不需要修改          
            'logistics_type'    => 'EXPRESS',//物流配送方式：POST(平邮)、EMS(EMS)、EXPRESS(其他快递)
            'logistics_fee'     => 0, //物流配送费用
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE', //物流费用付款方式：SELLER_PAY(卖家支付)、BUYER_PAY(买家支付)、BUYER_PAY_AFTER_RECEIVE(货到付款)           
            'seller_email'      => $payment['alipay_account'] //卖家邮箱，必填
        );
        ksort($parameter);
        reset($parameter);
        $param = '';
        $sign  = '';
        foreach ($parameter AS $key => $val){
            $param .= "$key=" .urlencode($val). "&";
            $sign  .= "$key=$val&";
        };
        $param = substr($param, 0, -1);
        $sign  = substr($sign, 0, -1). $payment['alipay_key'];
        $button = ' <input type="submit" class="confirm-address fl" onclick="window.open(\'https:/'.'/www.alipay.com/cooperate/gateway.do?'.$param. '&sign='.md5($sign).'&sign_type=MD5\')" value="立即支付">';
		return $button;
}

function dopayRe(){
	$error='';
	if (!empty($_POST)){
		   foreach($_POST as $key => $data){
		   $_GET[$key] = $data;
		   };
		};

		$sql='select * from '.$this->_prefix.'shoporderuser where id  = '.addslashes($_GET['out_trade_no']);
	    $order=$this->db->get_one($sql);

	if($order['id']=='')return "无法继续付款，无法找到订单!";
	if($order['ifpay']==1)return "请勿重复使用已支付的订单!";
	$sql='select * from '.$this->_prefix.'payment where pay_code   =\''.addslashes($_GET['pay']).'\'';
	$pay=$this->db->get_one($sql);
	$payment=unserialize($pay['pay_config']);	
	ksort($_GET);
	reset($_GET);
	$sign = '';
	foreach ($_GET AS $key=>$val){
		 if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'mod' && $key != 'pay' && $val != ""){
		      $sign .= "$key=$val&";
		 };
	};

	$sign = substr($sign, 0, -1) . $payment['alipay_key'];
	if (md5($sign) != $_GET['sign']){
	      return "支付失败!<br/>";
	}
		return $ary=array('price'=> $_GET['total_fee'] ,'orderid'=>$_GET['out_trade_no']);
}


function payRe(){
	$error='';
	if (!empty($_POST)){
		   foreach($_POST as $key => $data){
		   $_GET[$key] = $data;
		   };
		};

		$sql='select * from '.$this->_prefix.'order where orderid  = '.addslashes($_GET['out_trade_no']).' and userid ='.$GLOBALS['SCup']->SCup_user_msg['user_id'];
	$order=$this->db->get_one($sql);

	if($order['orderid']=='')return "无法继续付款，无法找到订单!";
	if($order['pay']==1)return "请勿重复使用已支付的订单!";
	$sql='select * from '.$this->_prefix.'payment where pay_code   =\''.addslashes($_GET['pay']).'\'';
	$pay=$this->db->get_one($sql);
	$payment=unserialize($pay['pay_config']);	
	ksort($_GET);
	reset($_GET);
	$sign = '';
	foreach ($_GET AS $key=>$val){
		 if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'mod' && $key != 'pay' && $val != ""){
		      $sign .= "$key=$val&";
		 };
	};

	$sign = substr($sign, 0, -1) . $payment['alipay_key'];
	if (md5($sign) != $_GET['sign']){
	      return "支付失败!<br/>";
	}
		return $ary=array('price'=> $_GET['total_fee'] ,'orderid'=>$_GET['out_trade_no']);
}

function addmymoney(){
	if (!empty($_POST)){
		foreach($_POST as $key => $data){
		   $_GET[$key] = $data;
		};
	};

	$sql='select * from '.$this->_prefix.'addmoney where id   =\''.addslashes($_GET['out_trade_no']).'\'';

	$pay=$this->db->get_one($sql);
	if($pay['id']!='')return '支付失败';
	$sql='select * from '.$this->_prefix.'payment where pay_code   =\''.addslashes($_GET['pay']).'\'';

	$pay=$this->db->get_one($sql);
	$payment=unserialize($pay['pay_config']);
	
	ksort($_GET);
	reset($_GET);
	$sign = '';
	foreach ($_GET AS $key=>$val){
		 if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'mod' && $key != 'pay'){
		      $sign .= "$key=$val&";
		 };
	};
	$sign = substr($sign, 0, -1) . $payment['alipay_key'];
	if (md5($sign) != $_GET['sign']){
	     return  "支付失败!<br/>";
	}
	return $ary=array('price'=> $_GET['total_fee'] ,'orderid'=>$_GET['out_trade_no']);
}


}